#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

#include <sys/mman.h>


//显示屏文件描述符
static int fb_fd = -1;


//指向帧缓冲区的首地址
static int* plcd = NULL;

void lcd_open(void)					//打开显示屏
{
	int fd;

	fd = open("/dev/fb0", O_RDWR);
	if (fd == -1)
	{
		printf("failed to open /dev/fb0\n");
		return ;
	}
	fb_fd = fd;

	//系统调用mmap()用于共享内存
	plcd = mmap(NULL, 800*480*4, PROT_WRITE, MAP_SHARED, fd, 0);	//PROT表示可写，800*480*4是映射到调用进程地址空间的字节数，
	if (plcd == MAP_FAILED)
	{
		printf("mmap error\n");
		return ;
	}

}


void lcd_close(void)				//关闭显示屏
{
	close(fb_fd);
	munmap(plcd, 800*480*4);
}


void lcd_draw_point(int x, int y, int color)			//画一个点
{	
	if (x >= 0 && x < 800 && y >= 0 && y < 480)
	{
		*(plcd + 800*y + x) = color;
	}
}


void lcd_draw_rectangle(int x0, int y0, int w, int h, int color)		//画一个矩形
{
	//画矩形：把矩形中所有像素点填充一个相同的颜色值
	//(x, y)
	//	x >= x0 &&　x < x0 + w  (1)
	//  y >= y0 &&　y < y0 + h  (2)

	int x, y;

	for (y = y0; y < y0 + h; y++)
	{
		for (x = x0; x < x0 + w; x++)
		{
			lcd_draw_point(x, y, color);
		}
	}
}

